$(function () {
    $("#jqGrid").jqGrid({
        url: '../contractinfo/searchList',
        datatype: "json",
        colModel: [
			{ label: '网签编码', name: 'netSignCode', index: 'netSignCode', width: 80 ,formatter: function (value, options, row){
                return value || '暂无';
            }},
			{ label: '房源编码', name: 'houseCode', index: 'houseCode', width: 80 }, 
			{ label: '出租人', name: 'landlordName', index: 'landlordName', width: 80 }, 
			{ label: '承租人', name: 'tenantName', index: 'tenantName', width: 80 }, 
			{ label: '租金', name: 'monthRent', width: 80, formatter: function (value, options, row){
				return value ? value + '元' : '';
	        }}, 
			{ label: '租赁时间', name: 'totalMonth', width: 80, formatter: function (value, options, row){
				return value ? value + '月' : '';
	        }}, 
			{ label: '租约状态', name: 'contractStatus', width: 80, formatter: function (value, options, row){
				var status = ['待签约', '生效中', '已作废'];
				return status[value];
	        }}, 
			{ label: '操作', name: 'func', width: 80, formatter: function(value, options, row){
				return '<a class="layui-btn layui-btn-sm btn-detail" id="'+row.id+'">查看</a>';
	        }}
        ],
		viewrecords: true,
		height: '100%',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth: true,
        multiselect: false,
        multipleSearch: true,
        pager: "#jqGridPager",
        search: true,
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        postData: {
        	filters:'',contractStatus: '0'
        },
        beforeSelectRow: function(rowid, e) {
		    return false;
		},
		onRightClickRow: function () {
		    return false;
		},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
			$('#jqGrid').off('contextmenu');
            $('.btn-detail').on('click', function () {
                var id = $(this).attr('id');
                if(!id){
                    return;
                }
                vm.showList = false;
                vm.title = "详情";

                vm.getInfo(id)
            });
            vm.q = {
                netSignCode: '',
                houseCode: '',
                landlordPhone: '',
                tenantPhone: ''         
            };
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			netSignCode: "",
			houseCode: "",
		},
		detail: {
			contractStatus: '',
			netSignCode: '',
		},
		showList: true,
		title: null,
		contractStatus: 0,
		contractInfo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		filterStatus: function (status) {
			// （0：待签约、1：已签约、2：已退租）
			vm.contractStatus = status;
			vm.q = {};
			var grid = $("#jqGrid");
            var postdata = grid.jqGrid('getGridParam','postData');
            postdata.netSignCode = '';
			postdata.houseCode = '';
            $.extend(postdata,{filters:'',contractStatus: status});
            grid.jqGrid('setGridParam', { search: true, postData: postdata });
            grid.trigger("reloadGrid",[{page:1}]);
		},
		contractDetail: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "详情";
            
            vm.getInfo(id)
		},
		getInfo: function(id){
			$.get("../contractinfo/info/"+id, function(r){
                if(r.code == 0){
                    vm.contractInfo = r.contractInfo;
                    if (vm.contractInfo.contractValidityStartTime &&
                    	vm.contractInfo.contractValidityEndTime) {
	                    vm.contractInfo.fmt_contractTime =
	                    		vm.contractInfo.contractValidityStartTime +
	                    		' - ' +
	                    		vm.contractInfo.contractValidityEndTime;
                    }
                    var status = [
                    	'待签约，待租客确认',
                    	'已签约',
                    	'已作废'
                    ];
                    vm.detail.contractStatus = status[vm.contractInfo.contractStatus*1];
                    vm.detail.netSignCode = vm.contractInfo.netSignCode || '暂无';
                }else{
                    alert(r.msg);
                }
            });
		},
		reload: function (event) {
			vm.showList = true;

			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{
                	'netSignCode':vm.q.netSignCode,
                	'houseCode':vm.q.houseCode,
                	'tenantPhone':vm.q.tenantPhone,
                	'landlordPhone':vm.q.landlordPhone,
                	'contractStatus': vm.contractStatus
                },
                page:page
            }).trigger("reloadGrid");
		},
		pdfview: function () {
			location.href = "../app/contract/query/pdf?contractId=" + vm.contractInfo.id;
		}
	}
});